package model.dao;


import javax.persistence.Query;
import javax.persistence.EntityManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import model.util.EntityManagerUtil;
import model.domain.Project;
import java.util.List;
import javax.persistence.PersistenceContext;


@Repository
@Transactional
public class ProjectDao {

	private static final Log log = LogFactory.getLog(ProjectDao.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void insert(Project transientInstance) {
		log.debug("inserting Project instance");
		try {
			entityManager = EntityManagerUtil.getEntityManager();
			entityManager.getTransaction().begin();
			entityManager.persist(transientInstance);
			entityManager.getTransaction().commit();
			log.debug("insert successful");
		} catch (RuntimeException re) {
			log.error("insert failed", re);
			throw re;
		}
	}

	public void delete(Project persistentInstance) {
		log.debug("deleting Project instance");
		try {
			entityManager = EntityManagerUtil.getEntityManager();
			entityManager.getTransaction().begin();
			persistentInstance = entityManager.merge(persistentInstance);
			entityManager.remove(persistentInstance);
			entityManager.getTransaction().commit();

			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void insertOrUpdate(Project detachedInstance) {
		log.debug("inserting or updating Project instance");
		try {
			entityManager = EntityManagerUtil.getEntityManager();
			entityManager.getTransaction().begin();
			detachedInstance = entityManager.merge(detachedInstance);
			entityManager.persist(detachedInstance);
			entityManager.getTransaction().commit();
			log.debug("insert or update successful");
		} catch (RuntimeException re) {
			log.error("insert or update failed", re);
			throw re;
		}
	}

	public Project findById(Integer id) {
		log.debug("getting Project instance with id: " + id);
		try {
			entityManager = EntityManagerUtil.getEntityManager();
			Project instance = entityManager.find(Project.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<Project> getAll() {
		log.debug("getting all Project instances");
		try {
			entityManager = EntityManagerUtil.getEntityManager();
			Query query = entityManager.createQuery("from Project");
			log.debug("get successful");
			return query.getResultList();
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

        public Project getFirst() {
		log.debug("getting all Project instances");
		try {
			entityManager = EntityManagerUtil.getEntityManager();
			Query query = entityManager.createQuery("from Project");
			log.debug("get successful");
                        if(!query.getResultList().isEmpty()) {
                            return (Project) query.getResultList().get(0);
                        }
                        return null;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
}
